<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/2/5
 * Time: 下午12:29
 */

$op = empty($_GPC['op']) ? 'list' : $_GPC['op'];
$setting = pdo_fetch("SELECT settings FROM " . tablename('uni_account_modules') . " WHERE module = :module AND uniacid = :uniacid", array(':module' => 'vj_bontrip', ':uniacid' => $_W['uniacid']));
$setting = iunserializer($setting['settings']);
if ($op == 'list') {
    //线路
    $pindex = max(1, intval($_GPC['page']));
    $psize = 2;
    $lines = pdo_fetchall("SELECT * FROM " . tablename('bontrip_line')
        . " WHERE deleted_at is null"
        . " ORDER BY sort ASC,id DESC LIMIT "
        . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('bontrip_line')
        . " WHERE deleted_at is null");

    $max_page = ceil($total / $psize);
    $is_next = true;

    if ($max_page <= $pindex) {
        $is_next = false;
    }
    $title = "知行";
    include $this->template('line');
} else if ($op == 'detail') {
    $id = intval($_GPC['id']);
    if (empty($id)) message('参数错误', $this->createMobileUrl('line'), 'error');
    $line = pdo_fetch("SELECT * FROM " . tablename('bontrip_line')
        . " WHERE id = '{$id}' and deleted_at is null");
    if (empty($line)) message('线路不存在或已下架', $this->createMobileUrl('line'), 'error');
    if (!empty($line['thumbs'])) {
        $line['thumbs'] = unserialize($line['thumbs']);
    }

    $line['product_desc'] = unserialize($line['product_desc']);
    $line['product_desc']['desc'] = explode('占位符', str_replace(PHP_EOL, '占位符', $line['product_desc']['desc']));
    if (!empty($line['product_desc']['trip'])) {
        //替换换行符
        foreach ($line['product_desc']['trip'] as $key => $val) {
            $line['product_desc']['trip'][$key]['trip_desc'] = explode('占位符', str_replace(PHP_EOL, '占位符', $line['product_desc']['trip'][$key]['trip_desc']));
        }
    }

    $line['price_desc'] = unserialize($line['price_desc']);
    if (!empty($line['price_desc']['price'])) {
        //替换换行符
        foreach ($line['price_desc']['price'] as $key => $val) {
            $line['price_desc']['price'][$key]['price_desc'] = explode('占位符', str_replace(PHP_EOL, '占位符', $line['price_desc']['price'][$key]['price_desc']));
        }
    }

    $line['buy_desc'] = unserialize($line['buy_desc']);
    if (!empty($line['buy_desc']['buy'])) {
        //替换换行符
        foreach ($line['buy_desc']['buy'] as $key => $val) {
            $line['buy_desc']['buy'][$key]['buy_desc'] = explode('占位符', str_replace(PHP_EOL, '占位符', $line['buy_desc']['buy'][$key]['buy_desc']));
        }
    }

    //获取酒店列表
    $hotels = pdo_fetchall("SELECT h.* FROM " . tablename('bontrip_hotel')
        . " AS h RIGHT JOIN " . tablename('bontrip_line_hotel') . " AS lh ON h.id = lh.hotel_id WHERE h.deleted_at is null AND lh.line_id = {$id}");
    if (!empty($hotels)) {
        foreach ($hotels as $key => $val) {
            $thumbs = unserialize($val['thumbs']);
            if(!empty($thumbs)){
                foreach ($thumbs as $k => $v){
                    $thumbs[$k] = tomedia($v);
                }
            }
            $hotels[$key]['thumbs'] = json_encode($thumbs);
        }
    }

    $title = "填写订单";
    include $this->template('line_detail');
}
